// pages/home/me.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    myinfo: {},
    uid: 0,
    avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    nickName: '点击显示微信头像',
    if_register: false,
    openid: '',
    session_key: '',
    eshop_id: '',
    amount_value: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init_data(options)
    // this.get_myinfo()
  },

  confirm_buying() {
    let params = {
      uid: this.data.uid,
      total_fee: this.data.amount_value,
      sid: this.data.eshop_id,
      type: 15,
    }
    util.showLoading("正在购买")
    request.post(api.orderadd, params).then(res => {
      if (res) {
        let post_data = {
          id: res
        }
        request.post(api.orderpay, post_data).then(res => {
          this.wx_pay(res)
        })
      }
      else {
        util.showToast('购买失败')
      }
    })
  },

  wx_pay(res) {
    let post_data = res

    wx.requestPayment({
      nonceStr: post_data.nonceStr,
      package: post_data.package,
      paySign: post_data.paySign,
      timeStamp: post_data.timeStamp,
      signType: post_data.signType,
      success(res) {
        util.showToast('购买成功')
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/shop/shop_center',
          })

        }, 1500);
      },
      fail(e) {
        util.showToast('开通取消');
      }
    })
  },

  amount_input(e) {
    this.setData({
      amount_value: e.detail.value
    })
  },

  init_data(options) {
    let uid = util.getAppValue(App.globalData.uid)
    let eshop_id = App.globalData.eshop_id
    let amount_value = options.is_vip == 0 ? '1000' : '10000'
    if (!uid) {
      uid = 1
    }
    this.setData({
      if_register: App.globalData.if_register,
      uid,
      options,
      eshop_id,
      amount_value,
    })
    let avatarUrl = App.globalData.userInfo.avatarUrl
    let nickName = App.globalData.userInfo.nickName
    if (avatarUrl) {
      this.setData({
        avatarUrl,
        nickName
      })
    }
  },

  go_order() {
    let url = '/pages/order/list' + '?from=manager'
    wx.navigateTo({
      url
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})